package group.sicher.log.model;

import group.sicher.log.util.TraceUtil;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Donald
 * @description
 * @date 2019-08-02
 **/
@Data
public class HttpRecord {
    private String requestId;
    private String path;
    private String header;
    private String parameters;
    private String method;
    private Long cost;
    private Integer status;
    private String requestBody;
    private String responseBody;
    private String ip;

    private HttpRecord() {
    }

    public static HttpRecord of(HttpServletRequest request, HttpServletResponse response, long cost) {
        HttpRecord record = new HttpRecord();
        record.requestId = TraceUtil.getRequestId();
        record.path = request.getRequestURI();
        record.header = TraceUtil.getRequestHeaders(request);
        record.method = request.getMethod();
        record.parameters = TraceUtil.getRequestParameters(request);
        record.requestBody = TraceUtil.getRequestBody(request);
        record.responseBody = TraceUtil.getResponseBody(response);
        record.status = response.getStatus();
        record.cost = cost;
        record.ip = TraceUtil.getIpAddress(request);
        return record;
    }
}
